package com.ruoyi.develop.lvFahuotongzhi.service.impl;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.lvFahuomingxi.domain.LvFahuomingxi;
import com.ruoyi.develop.lvFahuomingxi.service.impl.LvFahuomingxiServiceImpl;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvFahuotongzhi.mapper.LvFahuotongzhiMapper;
import com.ruoyi.develop.lvFahuotongzhi.domain.LvFahuotongzhi;
import com.ruoyi.develop.lvFahuotongzhi.service.ILvFahuotongzhiService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出货通知单Service业务层处理
 * @author qh
 * @date 2024-09-30
 */
@Service
public class LvFahuotongzhiServiceImpl implements ILvFahuotongzhiService {
    @Resource
    private LvFahuotongzhiMapper lvFahuotongzhiMapper;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private LvFahuomingxiServiceImpl fahuomingxiService;
    /**
     * 查询出货通知单
     * @param id 出货通知单主键
     * @return 出货通知单
     */
    @Override
    public LvFahuotongzhi selectById(String id) {
        return lvFahuotongzhiMapper.selectById(id);
    }

    /**
     * 查询出货通知单列表
     * @param entity 出货通知单
     * @return 出货通知单
     */
    @Override
    public List<LvFahuotongzhi> selectList(LvFahuotongzhi entity) {
        return lvFahuotongzhiMapper.selectList(entity);
    }
    /**
     * 新增出货通知单
     * @param entity 出货通知单
     * @return 结果
     */
    @Override
    public int insert(LvFahuotongzhi entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setCreateTime(DateUtils.getNowDate());
            return lvFahuotongzhiMapper.insert(entity);
    }
    /**
     * 修改出货通知单
     * @param entity 出货通知单
     * @return 结果
     */
    @Override
    public int update(LvFahuotongzhi entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return lvFahuotongzhiMapper.update(entity);
    }

    @Override
    public int updateState(String id,String status) {
        return lvFahuotongzhiMapper.updateState(id,status);
    }
    /**
     * 批量删除出货通知单
     * @param ids 需要删除的出货通知单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvFahuotongzhiMapper.deleteByIds(ids);
    }
    /**
     * 删除出货通知单信息
     * @param id 出货通知单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvFahuotongzhiMapper.deleteById(id);
    }
    /**
     * 发货单产品添加
     * @param fahuomingxi
     */
    @Override
    @Transactional
    public void insertBatch(LvFahuotongzhi fahuomingxi) {
        List<GoodsSale> rows = fahuomingxi.getColumns();
        if (!rows.isEmpty()) {
            for (GoodsSale entity : rows) {
                entity.setIsfahuo(BusinessConstants.FAHUO_1);//发货通知单
                //entity.setStatus("10");
                goodsSaleService.update(entity);
                LvFahuomingxi lvFahuomingxi=new LvFahuomingxi();
                lvFahuomingxi.setProId(entity.getProjectId());
                lvFahuomingxi.setProNo(entity.getProjectNo());
                lvFahuomingxi.setProdName(entity.getProjectName());
                lvFahuomingxi.setSaleId(entity.getId());
                lvFahuomingxi.setGoodsId(entity.getGoodsId());
                lvFahuomingxi.setGoodsType(entity.getGoodsType());
                lvFahuomingxi.setDanwei(entity.getUnit());
                lvFahuomingxi.setDanjian(entity.getGoodsUnitPrice());
                lvFahuomingxi.setName(entity.getGoodsName());
                lvFahuomingxi.setModels(entity.getGoodModel());
                lvFahuomingxi.setMingxi(entity.getGoodsBrand());
                lvFahuomingxi.setNos(entity.getGoodsCode());
                lvFahuomingxi.setShuliang(entity.getOrderCount());
                lvFahuomingxi.setShengyuCount(entity.getOrderCount());
                lvFahuomingxi.setMoney(entity.getOrderCount().multiply(entity.getGoodsUnitPrice()));
                lvFahuomingxi.setRemark(entity.getRemark());
                lvFahuomingxi.setSaleman(entity.getSaleman());
                fahuomingxiService.insert(lvFahuomingxi);
            }
        }
    }

}
